import { useUserStore } from "@/stores/user";
import type {
  RegisterInfo,
  LoginInfo,
  LoginResponse,
  UserInfo,
  ResetPasswordInfo,
} from "@/types/user";
import { userInfoDefault } from "@/types/user";
import service from "@/utils/request";
import { ElMessage } from "element-plus";

function register(data: RegisterInfo) {
  return service({
    url: "/register",
    method: "post",
    data: data,
  });
}

export async function apiUserRegister(data: RegisterInfo): Promise<boolean> {
  const rsp: any = await register(data);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message);
    }

    return true;
  } else {
    ElMessage.error((rsp.message && rsp.message != "") ? rsp.message : "注册失败，请重试");
    return false;
  }
}

function login(data: LoginInfo) {
  return service({
    url: "/login",
    method: "post",
    data: data,
  });
}

export async function apiUserLogin(data: LoginInfo): Promise<boolean> {
  const userStore = useUserStore();
  const rsp: any = await login(data);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message);
    }

    userStore.setLoginInfo(rsp.data as LoginResponse);
    return true;
  } else {
    ElMessage.error((rsp.message && rsp.message != "") ? rsp.message : "登录失败，请重试");
    return false;
  }
}

function logout(userId: string) {
  return service({
    url: `/logout/${userId}`,
    method: "get",
  });
}

export async function apiUserLogout(userId: string): Promise<boolean> {
  const rsp: any = await logout(userId);
  const userStore = useUserStore();

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message);
    }

    userStore.reset();
    return true;
  } else {
    ElMessage.error((rsp.message && rsp.message != "") ? rsp.message : "退出登录失败，请重试");
    return false;
  }
}

function resetPassword(data: ResetPasswordInfo) {
  return service({
    url: `/user/${data.id}/password`,
    method: "put",
    data: data,
  });
}

export async function apiResetPassword(
  data: ResetPasswordInfo
): Promise<boolean> {
  const rsp: any = await resetPassword(data);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message);
    }

    return true;
  } else {
    ElMessage.error((rsp.message && rsp.message != "") ? rsp.message : "密码重置失败，请重试");
    return false;
  }
}

function update(data: UserInfo) {
  return service({
    url: `/user/${data.user_id}`,
    method: "put",
    data: data,
  });
}

export async function apiUserUpdate(data: UserInfo): Promise<boolean> {
  const userStore = useUserStore();
  const rsp: any = await update(data);

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message);
    }

    userStore.setUserInfo(data);
    return true;
  } else {
    ElMessage.error((rsp.message && rsp.message != "") ? rsp.message : "更新用户信息失败，请重试");
    return false;
  }
}

function get(userId: string) {
  return service({
    url: `/user/${userId}`,
    method: "get",
  });
}

export async function apiUserGet(userId: string): Promise<UserInfo> {
  const rsp: any = await get(userId);
  const userStore = useUserStore();

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message);
    }

    userStore.setUserInfo(rsp.data as UserInfo);
    return rsp.data as UserInfo;
  } else {
    ElMessage.error((rsp.message && rsp.message != "") ? rsp.message : "用户查询失败，请重试");
    return userInfoDefault();
  }
}

function _delete(userId: string) {
  return service({
    url: `/user/${userId}`,
    method: "delete",
  });
}

export async function apiUserDelete(userId: string): Promise<boolean> {
  const rsp: any = await _delete(userId);
  const userStore = useUserStore();

  if (true == rsp.success) {
    if (rsp.message && rsp.message != "") {
      ElMessage.success(rsp.message);
    }

    userStore.reset();
    return true;
  } else {
    ElMessage.error((rsp.message && rsp.message != "") ? rsp.message : "用户删除失败，请重试");
    return false;
  }
}
